/*
 * Created on Aug 19, 2004
 *
 * To change the template for this generated file go to
 * Window&gt;Preferences&gt;Java&gt;Code Generation&gt;Code and Comments
 */
package view.listeners;

import algorithme.AlgorithmeBellmanFord;
import algorithme.AlgorithmeDijkstra;
import algorithme.AlgorithmeExplorationArborescente;
import test.Application;
import view.JCanvas;
import view.drawable.IMovableDrawable;
import view.drawable.MoveDrawableMouseListener;
import view.drawable.NoeudDrawable;

import javax.swing.*;
import java.awt.*;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseWheelEvent;
import java.awt.geom.AffineTransform;
import java.awt.geom.NoninvertibleTransformException;
import java.awt.geom.Point2D;

public class NonOverlapMoveAdapter extends MoveDrawableMouseListener implements KeyListener {

    private Point initialLocation;
    private static int prevN = 0;

    /**
     * @param canvas
     */
    public NonOverlapMoveAdapter(JCanvas canvas) {
        super(canvas);
        canvas.addKeyListener(this);
        canvas.addMouseWheelListener(this);
        canvas.setFocusable(true);
    }


    public void mouseReleased(MouseEvent e) {
        if(drawable.isEmpty()) {
            super.mouseReleased(e);
            return ;
        }
        for (IMovableDrawable drawable1 : drawable)
            if( !canvas.isAlone(drawable1)){
                drawable1.setPosition(initialLocation);
            }
        initialLocation =null;
        super.mouseReleased(e);
        canvas.repaint();
    }

    /* (non-Javadoc)
     * @see java.awt.event.MouseListener#mousePressed(java.awt.event.MouseEvent)
     */
    public void mousePressed(MouseEvent e) {
        super.mousePressed(e);


        if (SwingUtilities.isLeftMouseButton(e)) {
            if(!drawable.isEmpty()) {
                initialLocation = e.getPoint();
            }
        } else {
            for (IMovableDrawable drawable1 : drawable)
                if(drawable!=null && drawable1 instanceof NoeudDrawable) {
                    NoeudDrawable noeudDrawable = (NoeudDrawable)drawable1;
                    Application.getGraphS().setSource(noeudDrawable.getNoeud());
                }
        }
    }

    @Override
    public void keyTyped(KeyEvent e) { }

    @Override
    public void keyPressed(KeyEvent e) { }

    @Override
    public void keyReleased(KeyEvent e) {
        if (e.getKeyChar() == 'b') {
            Application.getGraphS().setCurrentAlgorithme(new AlgorithmeBellmanFord());
        } else if(e.getKeyChar() == 'd') {
            Application.getGraphS().setCurrentAlgorithme(new AlgorithmeDijkstra());
        } else if(e.getKeyChar() == 'e') {
            Application.getGraphS().setCurrentAlgorithme(new AlgorithmeExplorationArborescente());
        }
    }

    @Override
    public void mouseWheelMoved(MouseWheelEvent event) {
        super.mouseWheelMoved(event);
        int n = event.getWheelRotation();
        Point p1 = event.getPoint();
        JCanvas jCanvas = Application.getWindowS().getjCanvas();
        Point2D p2;

        try {
            p2 = new AffineTransform( ).inverseTransform(p1, null);
        } catch (NoninvertibleTransformException ex) {
            ex.printStackTrace();
            return;
        }

        jCanvas.setCenter_scale_x(p1.getX());
        jCanvas.setCenter_scale_y(p1.getY());
        jCanvas.setTranslate_scale_x(-p2.getX());
        jCanvas.setTranslate_scale_y(-p2.getY());
        jCanvas.setScale(Math.min(5, Math.max(0.1, jCanvas.getScale() - (.1 * n))));

        prevN = n;
        jCanvas.repaint();
    }
}
